#!/usr/bin/perl
use strict;


package server;
#use modules::encode;
sub hub {
 #   use modules::encode;
    my ($dbh,$input) = @_;
    my $sth;
    my @input = split(/ /,$input);
    chop $input[6]; chop $input[6]; chop $input[6];
    my $numencode = encode($input[6]);

    $sth = $dbh->prepare("INSERT INTO server VALUES ('$input[1]','$input[6]','$input[6]',$numencode,$numencode);");
    $sth->execute;
}
sub new {
   
    my ($dbh,$input) = @_;
    my $sth;
    my @input = split(/ /,$input);
    chop $input[7]; chop $input[7]; chop $input[7];
    my $encode1 = encode($input[7]); my $encode2 = encode($input[0]);
    $sth = $dbh->prepare("INSERT INTO server VALUES ('$input[2]','$input[7]','$input[0]',$encode1,$encode2);");
    $sth->execute;
}
sub squit {
    use modules::encode;
    my $dbh = shift; my $input = shift; my $sth;
    my @input = split(/ /,$input);
    $input = "";
    if ($input[3] eq '0') {
	$dbh->disconnect;
	system("sh restart");
	die "I kena squit la\n";
	exit(9);
    }
    else {
    #DELETE ALL NICKS AFFECTED.
    $sth = $dbh->prepare("DELETE FROM ninfo WHERE server = '$input[2]';");
    $sth->execute;
    #GET THAT SERVER's NUM
    $sth = $dbh->prepare("SELECT num FROM server WHERE name = '$input[2]';");
    $sth->execute;
    my $tmpnum = $sth->fetchrow_array;
    #GOT IT    

    #GET A LIST OF HUBS.
    my $encodehub = encode($tmpnum);
    $sth = $dbh->prepare("SELECT name FROM server WHERE hub = '$tmpnum' AND hubcode = '$encodehub';");
    undef $encodehub;
    $sth->execute;
    #WE GOT THE LIST OF SERVERS AFFECTED NOW SQUIT THEM.
    while (my @squits = $sth->fetchrow_array) {
	squit2($dbh,"ABC SQ $squits[0] 123546 :no reason");
    }

#WE CAN DELETE IT NOW 
    $sth = $dbh->prepare("DELETE FROM server WHERE name = '$input[2]';");
    $sth->execute;
}
}
sub squit2 {
    
    my ($dbh,$input) = @_;
    my $sth; my @input = split(/ /,$input); undef $input;
#DELETE NICKS FIRST
    $sth = $dbh->prepare("DELETE FROM ninfo WHERE server = '$input[2]';");
    $sth->execute;
#GET THE SQUIT SERVER's NUM
    $sth = $dbh->prepare("SELECT num FROM server WHERE name = '$input[2]';");
    $sth->execute;
    my $tmpnum = $sth->fetchrow_array;
#GET LIST OF SERVERS AFFECTED..
    my $hubcode =encode($tmpnum);
    $sth = $dbh->prepare("SELECT name FROM server WHERE hub = '$tmpnum' AND hubcode = '$hubcode';");
    $sth->execute;
    while (my @squits = $sth->fetchrow_array) {
	squit($dbh,"ABC SQ $squits[0] 2323434 :hi");
    }
    $sth = $dbh->prepare("DELETE FROM ninfo WHERE server = '$input[2]';");
    $sth->execute;
}
sub encode {
    my $code = $_[0];
    my @code = split(//,$code);
    my $return;
    foreach (@code) {
        if ($_ eq lc($_)) {
            $return .= '1';
        }
        else {
            $return .= '2';
        }
    }
    return $return;
}                
1;
